﻿package com.projects.xenia.database_classes;


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;

import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.zip.GZIPOutputStream;


import com.projects.xenia.global_modules.MainGlobalModule;



import android.database.Cursor;


public class SqlIntoXML {
	
	private static final String WebServiceCatalogURL = "http://"+MainGlobalModule.SettingsCollection.get("PrimaryWebServiceURL")+"/maWebService/";
	
	private String GetStringValueFromCursorField (Cursor MyCursor,int cols) {
				
		LinkedHashMap <String,String> RepTab = new LinkedHashMap <String, String>();
		
		RepTab.put("\"","&quot");
		RepTab.put("©","&copy");
		RepTab.put("®","&reg");
		RepTab.put("™","&trade");
		RepTab.put("?","&euro");
		RepTab.put("Ј","&pound");
		RepTab.put("„","&bdquo");
		RepTab.put("“","&ldquo");
		RepTab.put("«","&laquo");
		RepTab.put("»","&raquo");
		RepTab.put(">","&gt");
		RepTab.put("<","&lt");
		RepTab.put("≥","&ge");
		RepTab.put("≤","&le");
		RepTab.put("≈","&asymp");
		RepTab.put("≠","&ne");
		RepTab.put("≡","&equiv");
		RepTab.put("§","&sect");
		RepTab.put("&","&amp");
		RepTab.put("∞","&infin");		
		
		String Value = "";
		
		try {
			
			Value = MyCursor.getString(cols);
			
			for(String key : RepTab.keySet()) {
				String ret = RepTab.get(key);
				Value.replace(key, ret);
			}
			   
		} catch (Exception e0) {
			
			try {
				Value = String.valueOf(MyCursor.getInt(cols));			
			} catch (Exception e1) {
				try {
					Value = String.valueOf(MyCursor.getDouble(cols));			
				} catch (Exception e2) {
					
				}
			}
			
		}
		
		return (Value);
		
	}
	
	
	private Cursor GetQueryByTable (String MyTable) {		
		
		
		if (MyTable.toLowerCase().equals("xdoc_orders_h")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_orders_h where date(date_create) >= date('now') and state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			int a = 0;
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_orders_t")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_orders_t where hdoc_id in (select id from xdoc_orders_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_orders_d")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_orders_d where hdoc_id in (select id from xdoc_orders_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_orders_g")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_orders_g where hdoc_id in (select id from xdoc_orders_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}		
		
		if (MyTable.toLowerCase().equals("xdoc_orders_o")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_orders_o where hdoc_id in (select id from xdoc_orders_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}		

		if (MyTable.toLowerCase().equals("xdoc_orders_b")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_orders_b where hdoc_id in (select id from xdoc_orders_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}		

		
		if (MyTable.toLowerCase().equals("xdoc_payments_h")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_payments_h where date(date_create) >= date('now') and state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			int a = 0;
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_payments_t")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_payments_t where hdoc_id in (select id from xdoc_payments_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_payments_d")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_payments_d where hdoc_id in (select id from xdoc_payments_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_returns_h")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_returns_h where date(date_create) >= date('now') and state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			int a = 0;
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_returns_t")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_returns_t where hdoc_id in (select id from xdoc_returns_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_returns_d")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_returns_d where hdoc_id in (select id from xdoc_returns_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		//
		if (MyTable.toLowerCase().equals("xdoc_blanks_h")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_blanks_h where date(date_create) >= date('now') and state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			int a = 0;
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_blanks_t")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_blanks_t where hdoc_id in (select id from xdoc_blanks_h where date(date_create) >= date('now')  and state < 4)", null); 
			return (tablecursor);					
		}
		
		//
		
		if (MyTable.toLowerCase().equals("xdoc_gpslog")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_gpslog where date(date_create) >= date('now') and state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			return (tablecursor);					
		}		
		
		if (MyTable.toLowerCase().equals("xdoc_photoreports_h")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_photoreports_h where state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			int a = 0;
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_photoreports_t")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_photoreports_t where hdoc_id in (select id from xdoc_photoreports_h where state < 4)", null); 
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_photoreports_d")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select * from xdoc_photoreports_d where hdoc_id in (select id from xdoc_photoreports_h where state < 4)", null); 
			return (tablecursor);					
		}
	
		if (MyTable.toLowerCase().equals("xdoc_visits_h")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_visits_h where state < 4", new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()});
			int a = 0;
			return (tablecursor);					
		}
		
		if (MyTable.toLowerCase().equals("xdoc_readedmessages")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xdoc_readedmessages where state < 4",new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()}); 
			return (tablecursor);					
		}		
		
		if (MyTable.toLowerCase().equals("xsys_errors")==true) {
			Cursor tablecursor = MainGlobalModule.DBE.rawQuery("select *,? as deviceid from xsys_errors where state < 4",new String [] {MainGlobalModule.SettingsCollection.get("DeviceId").toString()}); 
			return (tablecursor);					
		}		
		
		
		
		return null;
		
	}
	
	public boolean TablesToXML (String [] Tables) {	
				
		StringBuffer xmlString = new StringBuffer ();
		
		xmlString.append("<root>");	
		
		for (String MyTable:Tables ) {
		
			Cursor MyCursor = GetQueryByTable (MyTable);
			
			if (MyCursor == null) {
				return (false);
			}
						
			if (MyCursor.getCount()>0) {
				
				xmlString.append(String.format("<%s>", MyTable));
			
				for (int rows = 0;MyCursor.getCount()>rows;rows++) {
				
					MyCursor.moveToPosition(rows);
					
					xmlString.append("<row");
				
					for (int cols = 0;MyCursor.getColumnCount()>cols;cols++) {
						xmlString.append(String.format(" %s=",MyCursor.getColumnName(cols)));
						xmlString.append("\"");
						xmlString.append (GetStringValueFromCursorField (MyCursor,cols));					
						xmlString.append("\"");
					}
				
					xmlString.append("/>");
				
				}
				xmlString.append(String.format("</%s>", MyTable));
			}
			
						
		}
		
		xmlString.append("</root>");
		
		try {

			URL xUrl = new URL(WebServiceCatalogURL.concat("XeniaPostHandler.ashx"));
			HttpURLConnection urlConnection = (HttpURLConnection) xUrl.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setChunkedStreamingMode(0);
			
			OutputStream xOutputStream = new BufferedOutputStream(urlConnection.getOutputStream());
			GZIPOutputStream xGZIPOutputStream = new GZIPOutputStream(xOutputStream);
			xGZIPOutputStream.write(xmlString.toString().getBytes());			
			xGZIPOutputStream.flush();
			xOutputStream.flush();			
			xGZIPOutputStream.close ();
			xOutputStream.close ();
			
		    BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
		    String decodedString;
		    while ((decodedString = in.readLine()) != null) {
		    		System.out.println(decodedString);
		    	}
		    in.close();
			
			urlConnection.disconnect();
			
			return (true);
			
			}
		
		catch (IOException e) {
			
			e.printStackTrace();
			
			return (false);
			
			 }		 	
	}
	

}
